package org.xenon.vote.dao.imp;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.xenon.vote.dao.IntDao;
import org.xenon.vote.model.pojo.po.Choice;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class ChoiceDao implements IntDao<Choice> {
    private final JdbcTemplate jdbcTemplate;

    public ChoiceDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void add(Choice choice) {
        String sql = "insert into `vote`.`choice` (`turn`, `detail`, `order`, `owner`, `score`) values (?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, choice.getTurn(), choice.getDetail(), choice.getOrder(), choice.getOwner(), choice.getScore());
    }

    @Override
    public void edit(Choice choice) {
        String sql = "update `vote`.`choice` set `turn` = ? , `detail` = ? , `order` = ?, `owner` = ? , `score` = ? where `id` = ?";
        jdbcTemplate.update(sql, choice.getTurn(), choice.getDetail(), choice.getOrder(), choice.getOwner(), choice.getScore(), choice.getId());
    }

    /**
     * 获取指定的投票选项
     * @param id ID
     */
    @Override
    public Choice get(int id) {
        String sql = "select * from `vote`.`choice` where `id` = ?";
        List<Choice> list = jdbcTemplate.query(sql, new Object[]{id}, ChoiceRowMapper.getInstance());
        if (list.size() == 0) return null;
        return list.get(0);
    }

    /**
     * @param turn 所属的轮次号
     * @return 如果找不到返回空List
     */
    @Override
    public List<Choice> getAll(int turn) {
        String sql = "select * from `vote`.`choice` where `turn` = ? order by `order` ";
        return jdbcTemplate.query(sql, new Object[]{turn}, ChoiceRowMapper.getInstance());
    }

    @Override
    public void del(Choice choice) {
        String sql = "delete FROM `vote`.`choice` WHERE `id` = ?";
        jdbcTemplate.update(sql, choice.getId());
    }

    @Override
    public void del(int id) {
        String sql = "delete FROM `vote`.`choice` WHERE `id` = ?";
        jdbcTemplate.update(sql, id);
    }

    /**
     * 自动以RowMapper
     * 提高性能, 使用单例模式
     */
    private static class ChoiceRowMapper implements RowMapper<Choice> {

        private static ChoiceRowMapper choiceRowMapper;
        @Override
        public Choice mapRow(ResultSet rs, int i) throws SQLException {
            return new Choice(rs.getInt("id"),
                    rs.getInt("turn"),
                    rs.getString("detail"),
                    rs.getInt("order"),
                    rs.getString("owner"),
                    rs.getInt("score"));
        }

        static ChoiceRowMapper getInstance(){
            if (choiceRowMapper == null){
                synchronized (ChoiceRowMapper.class){
                    if(choiceRowMapper==null){
                        choiceRowMapper = new ChoiceRowMapper();
                    }
                }
            }
            return choiceRowMapper;
        }
    }
}
